package makabakaquestionnaire.zutsoftware.framework.Task.quartzt.job;

import makabakaquestionnaire.zutsoftware.entity.Users;
import makabakaquestionnaire.zutsoftware.framework.email.EmailService;
import makabakaquestionnaire.zutsoftware.framework.email.SendEmailModel;
import makabakaquestionnaire.zutsoftware.system.entity.CourseProject;
import makabakaquestionnaire.zutsoftware.system.entity.CourseUser;
import makabakaquestionnaire.zutsoftware.system.service.ICourseProjectService;
import makabakaquestionnaire.zutsoftware.system.service.Impl.CourseServiceImpl;
import makabakaquestionnaire.zutsoftware.utils.String.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 消息提醒的任务
 */
@Component("MessagePush")
public class MessagePush {
    @Autowired
    public ICourseProjectService courseProjectService;
    @Autowired
    public CourseServiceImpl courseService;
    @Autowired
    public EmailService emailService;

    /**
     * @param courseId 从数据库中拿
     * courseId->userIds->userEmail->sendEmail
     */
    public void pushEmail(Integer courseId) {
        CourseUser courseUser = new CourseUser();
        courseUser.setCourseId(courseId.longValue());
        List<Users> users = courseService.selectUsersList(courseUser);
        List<CourseProject> courseProject = courseProjectService.selectCourseProjectByCourseId(courseId.longValue());
        String pro="";
        for (CourseProject project : courseProject) {
             pro += String.format("http://localhost:8080/write?key=%s", project.getProjectKey())+";"+'\n';
        }

        for (Users user : users) {
            sendEmail(user, pro);
        }
    }

    public void sendEmail(Users users, String project) {
        String email = users.getEmail();
        if (StringUtils.isNotEmpty(email) && email != " ") {
            SendEmailModel model = new SendEmailModel();
            model.setReceiver(email);
            String textBody = String.format("请及时填写，问卷链接-------%s", project);
            model.setTextBody(textBody);
            model.setTitle("教学评价填写消息推送");
            emailService.sendEmail(model);
        }
    }
    public void pushEmail() {
        System.out.println("无参数pushEmail");
    }
}
